package com.lzjn.code.web.service.impl;

import com.lzjn.code.common.constant.Constants;
import com.lzjn.code.common.model.PageModel;
import com.lzjn.code.web.dao.DataSourceDao;
import com.lzjn.code.web.entity.DataSource;
import com.lzjn.code.web.service.DataSourceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("DataSourceService")
public class DataSourceServiceImpl implements DataSourceService {

	@Autowired
	private DataSourceDao dataSourceDao;

	@Override
	public PageModel getByPage(DataSource dataSource, PageModel page) {
		
		return dataSourceDao.getByPage(dataSource, page);
	}
	
	@Override
	public List<DataSource> getList() {
		
		return dataSourceDao.getList();
	}
	

	@Override
	public DataSource getById(String id) {
		
		return dataSourceDao.getById(id);
	}

	@Override
	public String add(DataSource dataSource) throws Exception {
		
		dataSourceDao.add(dataSource);
		
		return Constants.MESSAGE_SAVE_SUCCESS;
	}

	@Override
	public String update(DataSource dataSource) throws Exception {
		
		if(dataSourceDao.getById(dataSource.getId()) != null) {
			
			dataSourceDao.update(dataSource);
			
			return Constants.MESSAGE_SAVE_SUCCESS;
		}
		else {
			
			return Constants.MESSAGE_DATA_NOT_EXIST;
		}
	}

	@Override
	public String delete(String ids) throws Exception {
		
		if(ids.contains(",")) {
			
			String[] ides = ids.split(",");
			
			for(String id : ides) {
				
				if(StringUtils.isNotEmpty(id) && dataSourceDao.getById(id) == null) {
					
					return Constants.MESSAGE_DATA_NOT_EXIST; 
				}
			}
			
			dataSourceDao.batchDelete(ids);
			
			return Constants.MESSAGE_DELETE_SUCCESS;
			
		} else {
			
			if(dataSourceDao.getById(ids) != null) {
				
				dataSourceDao.delete(ids);
				
				return Constants.MESSAGE_DELETE_SUCCESS;
			}
			else {
				
				return Constants.MESSAGE_DATA_NOT_EXIST;
			}
		}
	}

}
